/************************************************************************
 * The code is owned by mainframer.cn and Shang Tao
 * Without aproval of mainframer.cn and Shang Tao, removing the copyright
 * infomation is regarded as invassion of proprietary copyright
 ***********************************************************************/
package com.mr.qa;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLDecoder;
import java.util.Properties;
import java.util.TreeMap;

import com.mr.qa.bo.UserLevel;
import com.mr.qa.exception.QaException;


public class GlobalConfigs
{

    public final static String ORIGINAL_URL = "originalUrl";

    public static String smtp_server = "";
    public static String smtp_server_username = "";
    public static String smtp_server_password = "";
    public static String from_email_address = "";
    public static String mail_address = "";
    
    //hot question definition criteria, at least 2 answers and is viewed 2 times
    public static int SIZE_ANSWERS_HOT_QUESTION = 20;
    public static int SIZE_VIEW_HOT_QUESTION = 100;
    
    //the number of the shared files in shared file list 
    public static int SIZE_SHAREDFILE_LIST = 20;
    
    //if true, the user can not log on to the application
    //not used now
    public static boolean isApplicationUnderUpdate = false;
        
    //not user now
    public static String logsLocation = null;
    
    //user titles definition map
    public static TreeMap userLevelDefinitons = new TreeMap();
    
    
    //for FTP
    public static String ftp_server = "202.75.219.125";
    public static String ftp_server_port = "25";
    
    //will be deleted
    public static String ftp_server_file_location = "wwwroot"+File.separator
    +"webapps"+File.separator+"iask"+File.separator+"uploadedfiles";
    
    public static String ftp_server_user_name = "mainframer";
    
    public static String ftp_server_user_password = "*";

    //not used now
    public static int SIZE_LATESTNEWS_HOMEPAGE = 17;
    
    public static int SIZE_LATESTLIVES_HOMEPAGE = 18;
    
    public static int SIZE_PART_1_HOMEPAGE = 16;
    
    public static int SIZE_PART_2_HOMEPAGE = 15;
    
    public static int SIZE_PART_3_HOMEPAGE = 16;

    public static int SIZE_PART_4_HOMEPAGE = 16;
    
    public static int SIZE_GONGGAO_HOMEPAGE = 7;
    
    public static int SIZE_HELP_HOMEPAGE = 7;
    
    public static int SIZE_SHAREDFILES_HOMEPAGE = 18;//2008.12.9
    
    //the value will be filled in application filter
    
    public static String WEB_PHISICAL_ROOT_LOCATION = "";
    
    
    //the directory name where the uploaded files are placed
    public final static String UPLOADEDFILE_DIR = "uploaded";
    
    //the directory name where the uploaded image are placed
    public final static String UPLOADEDIMAGE_DIR = "uploaded" + File.separator + "image";
    
    
    //the directory name where the shared files are placed
    public final static String SHAREDFILE_DIR = "sharedfile";
    
    //the directory name where the header images are placed
    public final static String HEADERIMAGE_DIR = "header";
    
    //the directory name where the article images are placed
    public final static String ARTICLEIMAGE_DIR = "article";
    
    //how large a header image file can be
    public static int SIZE_HEADERIMAGE = 102400;//100*1024;
    
    //how large a image file can be
    public static int SIZE_IMAGE = 307200;//300*1024;
    
    //how large a uploaded file can be
    public static int SIZE_UPLOADEDFILE = 5242880;//5*1024*1024;
    
    //how large a article file can be
    public static int SIZE_ARTICLEIMAGE = 5242880;//300*1024;
    
    //how large a shared file can be
    public static int SIZE_SHAREDFILE = 5242880;//5*1024*1024;
    
    //how many times a question is viewed
    public static int Q_VIEW_COUNTS = 100;
    //how many answers a question has
    public static int Q_ANSWER_COUNTS = 20;
    
    //broadcast news everyone can view
    public static String BROADCAST_NEWS = "";
    
    //how many users are displayed in one page
    public static int SIZE_USER_LIST = 40;
    
    public static int SIZE_AWARD_LIST = 40;
    
    public static int SIZE_SHOP_ARTICLE_HISTORY_LIST = 40;
    
    //the origin of the news
    public static String NEWS_LIST_URL = "http://www.mainframer.cn/blog/default/?flavor=top&pagesize=5";
    //how many news are retrived on home page
    public static int NEWS_LIST_SIZE = 14;

    public static int SIZE_QUESTION_LIST = 25;//the number of questions in list page
     //the number of questions in list page fitered by category id
    public static int SIZE_CATEGORY_QUESTION_LIST = 25;
    
    public static int SIZE_LIFE_LIST = 25;//the number of lifes in list page
    //the number of lifes in list page fitered by category id
	public static int SIZE_CATEGORY_LIFE_LIST = 25;
	
	public static int SIZE_TECH_LIST = 25;//the number of tech in list page
	//the number of tech in list page fitered by category id
	public static int SIZE_CATEGORY_TECH_LIST = 25;
    
    
    //2008.5.30 how many questions are displayed on a user information page viewby bu others
    public static int SIZE_QUESTION_VIEWBYOTHER_LIST = 30;
    
    //2008.6.12 IBM alum categody ID
    public static int IBM_ALUM_CATEGORY_ID = 52;
    
    
    //2008.6.24 prohibitedWords
    //keeping the prohibited words in question title
    public static String PROHIBITED_WORDS = "QQ,qq";
    
    //keeping the prohibited user ids
    public static String PROHIBITED_USERS = "";//delimited with ,
    
    //how many hours a user can post question after registration
    public static int HOURS_ADDQUESTION_AFTER_REGISTRATION = 3;
    
    //how many hours a user can upload shared files after registration
    public static int HOURS_ADD_SHARED_FILE_AFTER_REGISTRATION = 3;
    
    public static String QUESTION_CATEGORY_ID_NAME_PAIRS = "";
    
    public static String QUESTION_CATEGORY_IDS = "";
    
    public static int NUMBER_QUESTIONS_A_USER_ONE_DAY = 5;

    public static int NUMBER_ANSWER_FOR_VIEW_QUESTION = 8;
    
    public static int NUMBER_IMAGE_A_USER_ONE_DAY = 10;
    
    public static int NUMBER_SHARED_FILE_A_USER_ONE_DAY = 5;
    
    public static int NUMBER_SHARED_FILE_A_USER_DOWNLOAD_ONE_DAY = 2;
    
    public static int NUMBER_SHARED_FILE_A_USER_DOWNLOAD_LIMIT_SCORE = 50;
    
    //not used
    public static int ALLMFS_ID = 1;
    //not used
    public static String ALLMFS_LOGIN_NAME = "allmfs";
    
    //the maximum of from or to messages which one can owns
    public static int MAX_MESSAGES = 30;
    
    //when category is changed, Category Tag refresh the category structre one screen
    //QuesionCategoryAction is to update the value
    public static boolean CATEGORYCHANGED= false;

    //2009.4.25 add question_expiry limit. Unit suppors Day/Month, Day is default
    public static int QUESTION_EXPIRY_NUMBER = 30;
    //2009.5.9 add answer expiry limit. Unit defaults to Day
    public static int ANSWER_EXPIRY_NUMBER = 30;
    //question cannot be replied. Unit defaults to Day
    public static int QUESTION_LOCK_NUMBER = 90;

    //not used now
    public static String QUESTION_EXPIRY_UNIT = "D";

    //2009.5.30
    public static int QUESTION_CAN_DELETE_DURATION_AFTER_ADD = 24;
    public static int ANSWER_CAN_DELETE_DURATION_AFTER_ADD = 24;
    
    public static String ADMIN_PASSWORD;
    
    public static int SHARED_FILE_MONEY;
    public static int WEEKLY_AUTO_AWARD_CRITERIA;
    
    public static int INTERVAL_REGISTER_DOWNLOAD = 3;
    
    public static String MONEY_AWARD_LOGIN_ID = "all";
    
    //how many article are listed
    public static int SHOP_ARTICLE_LIST_SIZE = 6;
    
    //how many article are listed in left navigate
    public static int SHOP_ARTICLE_LIST_SIZE_NAV = 10;
    
    
    //the max width allowed for image upload
    public static int UPLOAD_IMAGE_WIDTH = 566;
    
    //the most active user count in question
    public static int MOST_ACTIVE_USER_COUNT_QUESTION = 10;
    
    //the most active user count in tech
    public static int MOST_ACTIVE_USER_COUNT_TECH = 10;
    
    public static String SYSTEM_PROPERTY_FILE = "System.properties";
    
    //how many days will a life expire
    public static int DAYS_QUESTION_EXPIRE = 30;
    
    public enum UploadFileType {
    	attachment, image, header, article_img;
    	public static String getPhysicalPath(UploadFileType type){
    		String result = null;
    		switch(type){
    		case attachment:
    			result = GlobalConfigs.UPLOADEDFILE_DIR;
    			break;
    		case image:
    			result = GlobalConfigs.UPLOADEDIMAGE_DIR;
    			break;
    		case header:
    			result = GlobalConfigs.HEADERIMAGE_DIR;
    			break;
    		case article_img:
    			result = GlobalConfigs.ARTICLEIMAGE_DIR;
    			break;
    		default:
    			throw new RuntimeException("The uploaded file type is not supported!");
    		}
    		return result;
    	}
    	
    	public static int getMaxSize(UploadFileType type){
    		int result = 0;
    		switch (type) {
			case attachment:
				result = GlobalConfigs.SIZE_UPLOADEDFILE;
				break;
			case image:
				result = GlobalConfigs.SIZE_IMAGE;
				break;
			case header:
				result = GlobalConfigs.SIZE_HEADERIMAGE;
				break;
			case article_img:
				result = GlobalConfigs.SIZE_ARTICLEIMAGE;
			default:
				throw new RuntimeException("The uploaded file type is not supported!");
			}
    		return result;
    	}
    }

        static
        {
            //user level definition
            
            UserLevel ul = null;
            ul = new UserLevel();
            ul.setGroup("AP");
            ul.setId(1);ul.setLevel(1);
            ul.setStartScore(0);ul.setEndScore(1000);
            ul.setTitle("Application Engineer");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);

            ul = new UserLevel();
            ul.setGroup("AP");
            ul.setId(2);ul.setLevel(2);
            ul.setStartScore(1001);ul.setEndScore(3000);
            ul.setTitle("Senior Application Engineer");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            ul = new UserLevel();
            ul.setGroup("AP");
            ul.setId(3);ul.setLevel(3);
            ul.setStartScore(3001);ul.setEndScore(6000);
            ul.setTitle("Senior Application Engineer");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            ul = new UserLevel();
            ul.setGroup("AP");
            ul.setId(4);ul.setLevel(4);
            ul.setStartScore(6001);ul.setEndScore(10000);
            ul.setTitle("Senior Application Engineer");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);

            ul = new UserLevel();
            ul.setGroup("AP");
            ul.setId(5);ul.setLevel(5);
            ul.setStartScore(10001);ul.setEndScore(20000);
            ul.setTitle("Vice President");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            ul = new UserLevel();
            ul.setGroup("AP");
            ul.setId(6);ul.setLevel(6);
            ul.setStartScore(20001);ul.setEndScore(100000);
            ul.setTitle("President");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);

            ul = new UserLevel();
            ul.setId(7);ul.setLevel(1);
            ul.setStartScore(0);ul.setEndScore(1000);
            ul.setTitle("System Engineer");
            ul.setGroup("SP");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);

            ul = new UserLevel();
            ul.setGroup("SP");
            ul.setId(8);ul.setLevel(2);
            ul.setStartScore(1001);ul.setEndScore(3000);
            ul.setTitle("Senior System Engineer");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            ul = new UserLevel();
            ul.setGroup("SP");
            ul.setId(9);ul.setLevel(3);
            ul.setStartScore(3001);ul.setEndScore(6000);
            ul.setTitle("System Specialist");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            ul = new UserLevel();
            ul.setGroup("SP");
            ul.setId(10);ul.setLevel(4);
            ul.setStartScore(6001);ul.setEndScore(10000);
            ul.setTitle("System Manager");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);

            ul = new UserLevel();
            ul.setGroup("SP");
            ul.setId(11);ul.setLevel(5);
            ul.setStartScore(10001);ul.setEndScore(20000);
            ul.setTitle("Vice President");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            ul = new UserLevel();
            ul.setGroup("SP");
            ul.setId(12);ul.setLevel(6);
            ul.setStartScore(20001);ul.setEndScore(100000);
            ul.setTitle("President");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            ul = new UserLevel();
            ul.setId(13);ul.setLevel(1);
            ul.setStartScore(0);ul.setEndScore(1000);
            ul.setTitle("Business Analyst");
            ul.setGroup("BA");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);

            ul = new UserLevel();
            ul.setGroup("BA");
            ul.setId(14);ul.setLevel(2);
            ul.setStartScore(1001);ul.setEndScore(3000);
            ul.setTitle("Senior Business Analyst");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            ul = new UserLevel();
            ul.setGroup("BA");
            ul.setId(15);ul.setLevel(3);
            ul.setStartScore(3001);ul.setEndScore(6000);
            ul.setTitle("Business Consultant");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            ul = new UserLevel();
            ul.setGroup("BA");
            ul.setId(16);ul.setLevel(4);
            ul.setStartScore(6001);ul.setEndScore(10000);
            ul.setTitle("Business Manager");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);

            ul = new UserLevel();
            ul.setGroup("BA");
            ul.setId(17);ul.setLevel(5);
            ul.setStartScore(10001);ul.setEndScore(20000);
            ul.setTitle("Vice President");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            ul = new UserLevel();
            ul.setGroup("BA");
            ul.setId(18);ul.setLevel(6);
            ul.setStartScore(20001);ul.setEndScore(100000);
            ul.setTitle("President");
            userLevelDefinitons.put(ul.getGroup()+ul.getId(),ul);
            
            //System.out.println("userLevelDefinitons="+userLevelDefinitons);
            try{
            	getSysProperties();
            }catch(Exception e){
            	e.printStackTrace();
            }
        }
        
	public int size(){
		//return the number of entries
		return 0;
	}
	public Object getValueObject(String name){
		//return the value associated with parameter name
		return null;
	}
	public String getValueString(String name){
		//return the String value associated with parameter name
		return null;
	}
	public Integer getValueInteger(String name){
		// return the Integer value associated with parameter name;
		return null;
	}
	public Boolean getValueBoolean(String name) {
		// return the Boolean value associated with parameter name;
		return null;
	}

	public static boolean checkSmtpConfig() {
		return (smtp_server.length() > 0 && smtp_server_username.length() > 0
				&& smtp_server_password.length() > 0 && from_email_address
				.length() > 0);
	}
	
	private static void getSysProperties() throws QaException {

		InputStream is = null;
		try {
			//is = this.getClass().getClassLoader().getResourceAsStream(GlobalConfigs.SYSTEM_PROPERTY_FILE);
			String filePath = GlobalConfigs.class.getResource("/").getPath() + GlobalConfigs.SYSTEM_PROPERTY_FILE;
			filePath = URLDecoder.decode(filePath, "UTF-8");
			//filePath = filePath.replaceAll("%20", " ");
			is = new FileInputStream(filePath);
			Properties props = new Properties();
			props.load(is);
			GlobalConfigs.ADMIN_PASSWORD = props.getProperty("adminPassword");
			GlobalConfigs.SHARED_FILE_MONEY = Integer.parseInt(props.getProperty("sharedFileMoney"));
			GlobalConfigs.INTERVAL_REGISTER_DOWNLOAD = Integer.parseInt(props.getProperty("intervalRegisterDownload"));
			GlobalConfigs.MONEY_AWARD_LOGIN_ID = props.getProperty("moneyAwardLoginId");
			GlobalConfigs.WEEKLY_AUTO_AWARD_CRITERIA = Integer.parseInt(props.getProperty("weeklyAutoAwardCriteria"));
			GlobalConfigs.SHOP_ARTICLE_LIST_SIZE = Integer.parseInt(props.getProperty("shopArticleListSize"));
			GlobalConfigs.SHOP_ARTICLE_LIST_SIZE_NAV = Integer.parseInt(props.getProperty("shopArticleListSizeNav"));
			GlobalConfigs.mail_address = props.getProperty("mailAddress");
		} catch (IOException e) {
			throw new QaException(e);
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException ex) {
					throw new QaException(ex);
				}
			}
		}
	}
}
